// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/buffer_util.h>
#include <pollux/dwio/common/selective_column_reader_internal.h>
#include <pollux/dwio/common/selective_repeated_column_reader.h>
#include <pollux/dwio/dwrf/common/decoder_util.h>
#include <pollux/dwio/dwrf/reader/dwrf_data.h>

namespace kumo::pollux::dwrf {

class SelectiveListColumnReader
    : public dwio::common::SelectiveListColumnReader {
 public:
  SelectiveListColumnReader(
      const TypePtr& requestedType,
      const std::shared_ptr<const dwio::common::TypeWithId>& fileType,
      DwrfParams& params,
      common::ScanSpec& scanSpec);

  void resetFilterCaches() override {
    child_->resetFilterCaches();
  }

  void seekToRowGroup(int64_t index) override {
    dwio::common::SelectiveListColumnReader::seekToRowGroup(index);
    auto positionsProvider = formatData_->seekToRowGroup(index);
    length_->seekToRowGroup(positionsProvider);

    POLLUX_CHECK(!positionsProvider.hasNext());

    child_->seekToRowGroup(index);
    child_->setReadOffsetRecursive(0);
    childTargetReadOffset_ = 0;
  }

  void readLengths(int32_t* lengths, int32_t numLengths, const uint64_t* nulls)
      override {
    length_->next(lengths, numLengths, nulls);
  }

 private:
  std::unique_ptr<dwio::common::IntDecoder</*isSigned*/ false>> length_;
};

class SelectiveMapColumnReader : public dwio::common::SelectiveMapColumnReader {
 public:
  SelectiveMapColumnReader(
      const TypePtr& requestedType,
      const std::shared_ptr<const dwio::common::TypeWithId>& fileType,
      DwrfParams& params,
      common::ScanSpec& scanSpec);

  void resetFilterCaches() override {
    keyReader_->resetFilterCaches();
    elementReader_->resetFilterCaches();
  }

  void seekToRowGroup(int64_t index) override {
    dwio::common::SelectiveMapColumnReader::seekToRowGroup(index);
    auto positionsProvider = formatData_->seekToRowGroup(index);

    length_->seekToRowGroup(positionsProvider);

    POLLUX_CHECK(!positionsProvider.hasNext());

    keyReader_->seekToRowGroup(index);
    keyReader_->setReadOffsetRecursive(0);
    elementReader_->seekToRowGroup(index);
    elementReader_->setReadOffsetRecursive(0);
    childTargetReadOffset_ = 0;
  }

  void readLengths(int32_t* lengths, int32_t numLengths, const uint64_t* nulls)
      override {
    length_->next(lengths, numLengths, nulls);
  }

 private:
  std::unique_ptr<dwio::common::IntDecoder</*isSigned=*/false>> length_;
};

} // namespace kumo::pollux::dwrf
